<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
  <head>
    <base href="<%=basePath%>">
	<%@ include file="/base/jsp/include.jsp"%>
  </head>
  <body>
    <form id="form" method="post">
        <input name="id" class="mini-hidden" />
        <fieldset style="border:solid 1px #aaa;padding:10px;">
            <legend >添加笔记作者</legend>
            <div style="padding:5px;">
            <table class="editor_table">
            	<tr>
                    <td style="width:200px;">姓名：</td>
                    <td style="width:150px;">
                    	<input name="name" id="name" class="mini-textbox editor_len" required="true" vtype="maxLength:150" emptyText="请输入姓名"/>
                    </td>
                </tr>
               
                <tr>
                    <td style="width:200px;">作者描述：</td>
                    <td style="width:150px;">
                    	<textarea name="description" id="description" class="mini-textarea editor_len" required="true" emptyText="不同描述务必用英文冒号分隔"></textarea>
                    </td>
                </tr>
              
              	<tr>
                    <td style="width:70px;">性别：</td>
                    <td style="width:150px;">
                    <input name="sex" id="sex" value="0" required="true" class="mini-combobox" textField="text" valueField="id" emptyText="类型"
                        url="${path}/base/json/sex.json" allowInput="false" showNullItem="true" nullItemText="请选择..."/>
                    </td>
                </tr>
               
                <tr>
                    <td style="width:70px;">上传作者头像：</td>
                    <td style="width:150px;">
                    	<!--fileCountLimit:上限 multi:是否为多文件 showImage:是否为图片  fileType:文件类型 fileSize:文件大小限制 buttonText:按钮文本 -->
                      <input id="file_upload" fileCountLimit="1" multi="false" type="file" 
                      	showImage="true" onuploadsuccess="onUploadSuccess"  fileType="*.jpg;*.jpeg;*.png" fileSize="1MB" buttonText="上传图标"/>
                    </td>
                </tr>
                 <tr style="display:none;">
                    <td style="width:200px;">文件ID：</td>
                    <td style="width:150px;">
                    	<input name="img" id="img" class="mini-textbox" required="true" vtype="maxLength:100" emptyText="图片id"/>
                    </td>
                </tr>
                 <tr>
                    <td style="width:70px;">上传其他头像：</td>
                    <td style="width:150px;">
                    	<!--fileCountLimit:上限 multi:是否为多文件 showImage:是否为图片  fileType:文件类型 fileSize:文件大小限制 buttonText:按钮文本 -->
                      <input id="file_upload_ids" fileCountLimit="100" multi="true" type="file" multi="true"
                      	showImage="true" onuploadsuccess="onUploadSuccessImgs"  fileType="*.jpg;*.jpeg;*.png" fileSize="1MB" buttonText="上传图标"/>
                    </td>
                </tr>
                
                 <tr style="display:none;">
                    <td style="width:200px;">大神图片列表：</td>
                    <td style="width:150px;">
                    	<input name="otherImgs" id="otherImgs" class="mini-textbox" required="true" emptyText="图片id"/>
                    </td>
                </tr>
                <tr>
                    <td style="width:200px;">视频地址：</td>
                    <td style="width:150px;">
                    	<input name="video" id="video" class="mini-textbox editor_len" vtype="url" emptyText="请输入视频URL地址"/>
                    </td>
                </tr>
                 <tr>
                    <td style="width:70px;">上传视频封面：</td>
                    <td style="width:150px;">
                    	<!--fileCountLimit:上限 multi:是否为多文件 showImage:是否为图片  fileType:文件类型 fileSize:文件大小限制 buttonText:按钮文本 -->
                      <input id="file_upload_video" fileCountLimit="1" multi="false" type="file" 
                      	showImage="true" onuploadsuccess="onUploadSuccessVideo"  fileType="*.jpg;*.jpeg;*.png" fileSize="1MB" buttonText="上传图标"/>
                    </td>
                </tr>
                 <tr style="display:none;">
                    <td style="width:200px;">文件ID：</td>
                    <td style="width:150px;">
                    	<input name="videoCover" id="videoCover" class="mini-textbox" vtype="maxLength:100" emptyText="图片id"/>
                    </td>
                </tr>
                
            </table>            
         </div>
        </fieldset>
        
        <div style="text-align:center;padding:10px;">
            <a class="mini-button" onclick="onOk" style="width:60px;margin-right:20px;">确定</a>       
            <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>    
            <a class="mini-button" onclick="onClearImg" style="width:100px;margin-right:20px;">清除头像</a>
            <a class="mini-button" onclick="onClearVideo" style="width:100px;margin-right:20px;">清除视频封面</a> 
        </div>        
    </form>
    
    <script type="text/javascript">
    	var id = '';
        mini.parse();
        var form = new mini.Form("form");
        function SaveData() {
			var o = form.getData();
            form.validate();
            if (form.isValid() == false) return;
            var json = mini.encode(o);
            
            $.ajax({
                url: "saveNoteAuthor.do",
                type: 'post',
                data: $.evalJSON(json),
                cache: false,
                success: function (text) {
                	if(text.indexOf('true') > -1){
				    	mini.alert("操作成功");
				    	CloseWindow("save");
				    }else{
				    	mini.alert("操作失败");
				    	CloseWindow();
				    }
                },
                error: function (textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                    CloseWindow();
                }
            });
        }
        
        //标准方法接口定义
        function SetData(data) {
            if (data.action == "edit") {
                //跨页面传递的数据对象，克隆后才可以安全使用
                data = mini.clone(data);
                $.ajax({
                    url: "findByIdNoteAuthor.do?id=" + data.id,
                    cache: false,
                    success: function (text) {
                        var o = mini.decode(text);
                        form.setData(o);
                        form.setChanged(false);
                        id = o.id;
                        var imgs = mini.get("img").getValue();
                        if(imgs != ''){
               			 $('#file_upload').setFileId(imgs,true,true);
               		    }
               		    otherImgs = mini.get("otherImgs").getValue();
               		    if(otherImgs != ''){
               			 $('#file_upload_ids').setFileId(otherImgs,true,true);
               		    }
               		    imgs = mini.get("videoCover").getValue();
               		    if(imgs != ''){
               			 $('#file_upload_video').setFileId(imgs,true,true);
               		    }
                    }
                });
            }
        }

        function GetData() {
            var o = form.getData();
            return o;
        }
        
        function CloseWindow(action) {
            if (action == "cancel" && form.isChanged()) {
                if (confirm("数据被修改了，是否先保存？")) {
                	SaveData();
                	return false;
                }
            }
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();            
        }
        
        function onOk(e) {
        	var imgId = $("#file_upload").getFileId();
        	mini.get("img").setValue(imgId);
        	if(imgId == ""){
        		mini.alert("请上传作者头像");
        		return ;
        	}
        	var otherImgs =  $("#file_upload_ids").getFileId();
        	mini.get("otherImgs").setValue(otherImgs);
        	var video = mini.get("video").getValue();
        	var videoCover = mini.get("videoCover").getValue();
        	if(video.length > 0 && videoCover.length <= 0){
        		mini.alert("如果有介绍视频，请务必添加视频封面");
        		return ;
        	}
            SaveData();
        }
        function onCancel(e) {
            CloseWindow("cancel");
        }
        function onClearImg(e){
            $("#file_upload").clearFileId();
        	mini.get("img").setValue("");
        }
        function onClearVideo(e){
            $("#file_upload_video").clearFileId();
        	mini.get("videoCover").setValue("");
        }
        function onUploadSuccess(e) {
             mini.get("img").setValue($("#file_upload").getFileId());
        }
        function onUploadSuccessVideo(e) {
             mini.get("videoCover").setValue($("#file_upload_video").getFileId());
        }
    </script>
  </body>
</html>
